﻿namespace DateAccessQAWebpart
{
    using System;
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class DepartmentInformationBL
    {
        #region Fields

        List<DepartmentFields> _AvailableFields;
        List<DepartmentInformation> _DepartmentList;
        List<DepartmentInformation> _FixedDepartmentList;
        List<DepartmentFields> _StoresDepartmentFields;

        #endregion Fields

        #region Properties

        public List<DepartmentFields> AvailableFields
        {
            get
            {
                if ( _AvailableFields == null )
                    _AvailableFields = new List<DepartmentFields>( );
                return this._AvailableFields;
            }
            set
            {
                this._AvailableFields = value;
            }
        }

        public List<DepartmentInformation> DepartmentList
        {
            get
            {
                if ( _DepartmentList == null )
                    _DepartmentList = new List<DepartmentInformation>( );
                return this._DepartmentList;
            }
            set
            {
                this._DepartmentList = value;
            }
        }

        public List<DepartmentInformation> FixedDepartmentList
        {
            get
            {
                if ( _FixedDepartmentList == null )
                    _FixedDepartmentList = new List<DepartmentInformation>( );
                return _FixedDepartmentList;
            }
            set
            {
                this._FixedDepartmentList = value;
            }
        }

        public List<DepartmentFields> StoresDepartmentFields
        {
            get
            {
                if ( _StoresDepartmentFields == null )
                    _StoresDepartmentFields = new List<DepartmentFields>( );
                return this._StoresDepartmentFields;
            }
            set
            {
                this._StoresDepartmentFields = value;
            }
        }

        #endregion Properties

        #region Methods

        public void GetMetaDataFileds( string siteURL, string sourceDepartment, string targetDepartment )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentFields departmentFields = new DepartmentFields( );
                TargetFieldsDataCollectionBL targetFieldsDataCollectionBL = new TargetFieldsDataCollectionBL( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Stores Detail" ];
                    foreach ( SPField sPField in sPList.Fields )
                    {
                        if ( !sPField.Hidden && sPField.FieldTypeDefinition.UserCreatable )
                        {
                            departmentFields = new DepartmentFields( );
                            departmentFields.DepartmentName = "Stores Detail";
                            departmentFields.FieldName = sPField.Title.Trim( );
                            departmentFields.InternalFieldName = sPField.InternalName.Trim( );
                            if ( !targetFieldsDataCollectionBL.IsFieldSelected( siteURL, targetDepartment, sourceDepartment, departmentFields.FieldName ) )
                            {
                                AvailableFields.Add( departmentFields );
                            }
                        }
                    }
                }
            }
        }

        public void GetNonDepartmentFields( string siteURL, string sourceDepartment, string targetDepartment )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentFields departmentFields = new DepartmentFields( );
                TargetFieldsDataCollectionBL targetFieldsDataCollectionBL = new TargetFieldsDataCollectionBL( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Administration List" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value>   </Eq></Where>";
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count > 0 )
                    {
                        foreach ( SPItem sPItem in items )
                        {
                            SPList sPList2 = sPWeb.Lists[ sPItem[ "Department List Name" ].ToString( ).Trim( ) ];
                            foreach ( SPField sPField in sPList2.Fields )
                            {
                                if ( !sPField.Hidden && sPField.FieldTypeDefinition.UserCreatable )
                                {
                                    departmentFields = new DepartmentFields( );
                                    departmentFields.DepartmentName = sPItem[ "Department Name" ].ToString( ).Trim( );
                                    departmentFields.FieldName = sPField.Title.Trim( );
                                    departmentFields.InternalFieldName = sPField.InternalName.Trim( );
                                    if ( !targetFieldsDataCollectionBL.IsFieldSelected( siteURL, targetDepartment, sourceDepartment, departmentFields.FieldName ) )
                                    {
                                        AvailableFields.Add( departmentFields );
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        this.GetMetaDataFileds( siteURL, sourceDepartment, targetDepartment );
                    }
                }
            }
        }

        public void LoadDepartments( string siteURL, bool fixedData )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                List<DepartmentInformation> list = new List<DepartmentInformation>( );
                DepartmentInformation departmentInformation = new DepartmentInformation( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Administration List" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = "<OrderBy><FieldRef Name='Department_x0020_Name' Ascending='True' /></OrderBy>";
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    foreach ( SPListItem sPListItem in items )
                    {
                        departmentInformation = new DepartmentInformation( );
                        if ( sPListItem[ "Department Name" ] != null )
                        {
                            departmentInformation.DepartmentName = sPListItem[ "Department Name" ].ToString( );
                        }
                        if ( sPListItem[ "Department List Name" ] != null )
                        {
                            departmentInformation.ListName = sPListItem[ "Department List Name" ].ToString( );
                        }
                        if ( sPListItem[ "Department Security Group" ] != null )
                        {
                            departmentInformation.SecurityGroup = sPListItem[ "Department Security Group" ].ToString( );
                        }
                        if ( fixedData )
                        {
                            FixedDepartmentList.Add( departmentInformation );
                        }
                        else
                        {
                            DepartmentList.Add( departmentInformation );
                        }
                    }
                    if ( fixedData )
                    {
                        DepartmentInformation departmentInformation2 = new DepartmentInformation( );
                        departmentInformation2.DepartmentName = "Stores Detail";
                        departmentInformation2.ListName = "Stores Detail";
                        FixedDepartmentList.Add( departmentInformation2 );
                    }
                }
            }
        }

        #endregion Methods
    }
}